<?php
/**
 * Created by PhpStorm.
 * User: wodrow
 * Date: 17-6-13
 * Time: 下午4:15
 */

namespace console\modules\phone\controllers;


use common\components\tools\Tools;
use console\models\call_rec\SlCallRec;
use yii\base\ErrorException;
use yii\console\Controller;

class CallController extends Controller
{
    public function actionRestoreAllRecordDb($date_start = 20000101, $date_end = 29991231)
    {
        $ftp = \Yii::$app->ftp_call_WC;
        $dirs = $ftp->listContents();
        foreach ($dirs as $k => $v){
            if (substr($v['filename'], 0, 2) == '20' && $v['type'] == 'dir' && is_numeric($v['filename']) && $v['filename'] > $date_start && $v['filename'] < $date_end){
                $_t = substr($v['filename'], 0, 4) . "-" . substr($v['filename'], 4, 2) . "-" . substr($v['filename'], 6, 2);
                var_dump($_t);
                SlCallRec::restoreDb($_t);
                sleep(1);
            }
        }
    }

    public function actionCopyAllDir($date_start = 20000101, $date_end = 29991231)
    {
        SlCallRec::copySourceDirToTarget($date_start, $date_end);
    }

    /**
     * php yii phone/call/restore-daily-record-db
     */
    public function actionRestoreDailyRecordDb()
    {
        $_t = time()-86400;
//        $_t = strtotime("2016-09-14");
        $_t = date("Y-m-d", $_t);
        var_dump($_t);
        SlCallRec::restoreDb($_t);
    }

    /**
     * php yii phone/call/restore-weekly-record-db
     */
    public function actionRestoreWeeklyRecordDb()
    {
        $_t = time()-86400*8;
        $_t1 = time();
        $this->actionRestoreAllRecordDb(date("Ymd", $_t), date("Ymd", $_t1));
    }

    /**
     * php yii phone/call/copy-weekly-record-files
     */
    public function actionCopyWeeklyRecordFiles()
    {
        $_t = time()-86400*8;
        $_t1 = time();
        $this->actionCopyAllDir(date("Ymd", $_t), date("Ymd", $_t1));
    }

    /**
     * php yii phone/call/filter-store-weekly-record-db-and-files
     * 保存有用的通话数据及文件
     */
    public function actionFilterStoreWeeklyRecordDbAndFiles()
    {
        $_t = time()-86400*7;
        $_t1 = time();
        $this->actionFilterStoreRecordDbAndFiles(date("Ymd", $_t), date("Ymd", $_t1));
    }

    /**
     * php yii phone/call/filter-store-record-db-and-files
     * @param int $date_start
     * @param int $date_end
     */
    public function actionFilterStoreRecordDbAndFiles($date_start = 20000101, $date_end = 29991231)
    {
        $s = strtotime($date_start);
        $e = strtotime($date_end);
        for ($t = $s; $t < $e; $t = $t + 86400) {
            $d = date("Ymd", $t);
            $HA = SlCallRec::REC_HA;
            $ha = strtolower($HA);
            SlCallRec::filterStoreDbAndFiles($t, \Yii::$app->ftp_call_HA, $d . "/", \Yii::$app->sftp_99_7, "Voice/{$HA}/", $ha, $HA);
        }
        for ($t = $s; $t < $e; $t = $t + 86400) {
            $d = date("Ymd", $t);
            $HB = SlCallRec::REC_HB;
            $hb = strtolower($HB);
            SlCallRec::filterStoreDbAndFiles($t, \Yii::$app->ftp_call_HB, $d."/", \Yii::$app->sftp_99_7, "Voice/{$HB}/", $hb, $HB);
        }
        for ($t = $s; $t < $e; $t = $t + 86400) {
            $d = date("Ymd", $t);
            $WC = SlCallRec::REC_WC;
            $wc = strtolower($WC);
            SlCallRec::filterStoreDbAndFiles($t, \Yii::$app->ftp_call_WC, $d."/", \Yii::$app->sftp_99_7, "Voice/{$WC}/", $wc, $WC);
        }
    }

    public function actionTest($date_start = 20000101, $date_end = 20000102)
    {
        $s = strtotime($date_start);
        $e = strtotime($date_end);
        for ($t = $s; $t < $e; $t = $t + 86400){
            $d = date("Ymd", $t);
            $WC = SlCallRec::REC_WC;
            $wc = strtolower($WC);
            SlCallRec::restoreDbAll($t, \Yii::$app->ftp_call_WC, $d."/", \Yii::$app->sftp_99_7, "Voice/{$WC}/", $wc, $WC);
//            $HA = SlCallRec::REC_HA;
//            $ha = strtolower($HA);
//            SlCallRec::filterStoreDbAndFiles($t, \Yii::$app->ftp_call_HA, $d."/", \Yii::$app->sftp_99_7, "Voice/{$HA}/", $ha, $HA);
//            $HB = SlCallRec::REC_HB;
//            $hb = strtolower($HB);
//            SlCallRec::filterStoreDbAndFiles($t, \Yii::$app->ftp_call_HB, $d."/", \Yii::$app->sftp_99_7, "Voice/{$HB}/", $hb, $HB);
        }
    }

    public function actionTest1()
    {
        ini_set('memory_limit',-1);
        /**
         * @var SlCallRec[] $wc_recs
         */
        $old = 'http://rec.weice.qiche100.com/main/download?path=/media/sda/voice/';
//        $old = 'http://rec.ysjr.qiche100.com/bf531/voice/';
        $new = 'Voice/WC/';
//        $new = '';
        $wc_recs = SlCallRec::find()->where(['rec_local'=>SlCallRec::REC_WC])->andWhere(['like', 'rec_file', $old.'%', false])->all();
        foreach ($wc_recs as $k => $v){
            $v->rec_file = $new.str_replace($old, '', $v->rec_file);
            $w = $v->toArray();
            unset($w['rec_id']);
            if (SlCallRec::findOne($w)){
                if ($v->delete()){
                    echo 'delete ';
                }else{
                    var_dump($v->errors);
                }
            }else{
                if ($v->save(false)){
                    echo 'save ';
                }else{
                    var_dump($v->errors);
                }
            }
        }
    }
}